# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3 5.4"
pkgname=lua-lgi
_rockname="${pkgname#lua-*}"
pkgver=0.9.2_git20230827
_gitcommit=e9290601d78470e84ed1e2f16b7c9a8834cd8bb5
pkgrel=0
pkgdesc="Dynamic Lua binding to GObject libraries using GObject-Introspection"
url="https://github.com/lgi-devs/lgi"
arch="all"
license="MIT"
makedepends="glib-dev gobject-introspection-dev libffi-dev"
checkdepends="dbus gtk+3.0 xvfb-run"
subpackages="$pkgname-doc"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-$_rockname:_split"
done
source="lgi-$pkgver.tar.gz::https://github.com/lgi-devs/lgi/archive/$_gitcommit.tar.gz
	tests.patch
	"
builddir="$srcdir/lgi-$_gitcommit"

case "$CARCH" in
	# Integer overflow in tests
	armhf|armv7|x86) options="$options !check" ;;
esac

prepare() {
	default_prepare

	for _i in $_luaversions; do
		cp -r . "$srcdir"/build-$_i
	done
}

build() {
	for _i in $_luaversions; do
		msg "build for $_i"
		make -C "$srcdir"/build-$_i PREFIX=/usr LUA_VERSION=$_i \
			LUA_CFLAGS="$(pkg-config --cflags lua$_i)"
	done
}

check() {
	for _i in $_luaversions; do
		msg "check for $_i"
		xvfb-run -a make -C "$srcdir"/build-$_i LUA=lua$_i \
			LDFLAGS="$LDFLAGS $(pkg-config --libs lua$_i)" \
			LUA_CFLAGS="$(pkg-config --cflags lua$_i)" check
	done
}

package() {
	for _i in $_luaversions; do
		cd "$srcdir"/build-$_i
		make install PREFIX=/usr DESTDIR="$pkgdir" LUA_VERSION=$_i
		local rockdir="$pkgdir"/usr/lib/luarocks/rocks-$_i/lua-lgi/$pkgver-1/
		mkdir -p "$rockdir"
		echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
	done
	cd "$builddir"
	mkdir -p "$pkgdir"/usr/share/doc/lua-lgi
	cp -r docs/* samples "$pkgdir"/usr/share/doc/lua-lgi/
}

_split() {
	_luaversion="${subpkgname%-"$_rockname"}"
	_luaversion="${_luaversion#lua*}"
	pkgdesc="Lua $_luaversion binding to GObject libraries using introspection"
	install_if="lua$_luaversion $pkgname=$pkgver-r$pkgrel"
	depends="$depends lua$_luaversion"

	amove usr/lib/lua/$_luaversion
	amove usr/lib/luarocks/rocks-$_luaversion
	amove usr/share/lua/$_luaversion
}

sha512sums="
e3025f55dafcb8b50596be03e3dea1c0a9b0cbea66cf0861ea0165eb0409ea1bcc27eb9b6db4ab3363c455971b0d434af263e15151eddf42d030ad3ca58f43e6  lgi-0.9.2_git20230827.tar.gz
b2a6d6c168bcbf91d88829b1c8bde5f14c03a0f81ee14fdd7caaebcb0622b680425eac73134722ac3c4a59b733365f549bf68b0084df697a0b63ac68b5085ba9  tests.patch
"
